/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jFBairros.java
 *
 * Created on 21/05/2012, 12:07:02
 */

package ecommercial;

import ecommercial.Classes.Bairros;
import ecommercial.Classes.Cidades;
import ecommercial.Classes.ClassConecta;
import ecommercial.Classes.ComboItem;
import ecommercial.Classes.Estados;
import ecommercial.Classes.Funcoes;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vitiazze
 */
public class jFBairros extends javax.swing.JDialog {

    /** Creates new form jFBairros */
    public jFBairros() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        buscarjButton = new javax.swing.JButton();
        novojButton = new javax.swing.JButton();
        editarjButton = new javax.swing.JButton();
        salvarjButton = new javax.swing.JButton();
        salvarMaisjButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        alterarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        sairjButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        bairrojTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        paisjTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        estadojComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cidadejComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cidadejButton = new javax.swing.JButton();
        atualizaCidadejButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gradeBairrosjTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ecommercial.EcommercialApp.class).getContext().getResourceMap(jFBairros.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jToolBar1.setBackground(resourceMap.getColor("jToolBar1.background")); // NOI18N
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        buscarjButton.setBackground(resourceMap.getColor("buscarjButton.background")); // NOI18N
        buscarjButton.setIcon(resourceMap.getIcon("buscarjButton.icon")); // NOI18N
        buscarjButton.setText(resourceMap.getString("buscarjButton.text")); // NOI18N
        buscarjButton.setFocusable(false);
        buscarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buscarjButton.setName("buscarjButton"); // NOI18N
        buscarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(buscarjButton);

        novojButton.setBackground(resourceMap.getColor("novojButton.background")); // NOI18N
        novojButton.setIcon(resourceMap.getIcon("novojButton.icon")); // NOI18N
        novojButton.setText(resourceMap.getString("novojButton.text")); // NOI18N
        novojButton.setFocusable(false);
        novojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        novojButton.setName("novojButton"); // NOI18N
        novojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        novojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(novojButton);

        editarjButton.setBackground(resourceMap.getColor("editarjButton.background")); // NOI18N
        editarjButton.setIcon(resourceMap.getIcon("editarjButton.icon")); // NOI18N
        editarjButton.setText(resourceMap.getString("editarjButton.text")); // NOI18N
        editarjButton.setFocusable(false);
        editarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editarjButton.setName("editarjButton"); // NOI18N
        editarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(editarjButton);

        salvarjButton.setBackground(resourceMap.getColor("salvarjButton.background")); // NOI18N
        salvarjButton.setIcon(resourceMap.getIcon("salvarjButton.icon")); // NOI18N
        salvarjButton.setText(resourceMap.getString("salvarjButton.text")); // NOI18N
        salvarjButton.setFocusable(false);
        salvarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        salvarjButton.setName("salvarjButton"); // NOI18N
        salvarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        salvarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(salvarjButton);

        salvarMaisjButton.setBackground(resourceMap.getColor("salvarMaisjButton.background")); // NOI18N
        salvarMaisjButton.setIcon(resourceMap.getIcon("salvarMaisjButton.icon")); // NOI18N
        salvarMaisjButton.setText(resourceMap.getString("salvarMaisjButton.text")); // NOI18N
        salvarMaisjButton.setFocusable(false);
        salvarMaisjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        salvarMaisjButton.setName("salvarMaisjButton"); // NOI18N
        salvarMaisjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        salvarMaisjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarMaisjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(salvarMaisjButton);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        alterarjButton.setBackground(resourceMap.getColor("alterarjButton.background")); // NOI18N
        alterarjButton.setIcon(resourceMap.getIcon("alterarjButton.icon")); // NOI18N
        alterarjButton.setText(resourceMap.getString("alterarjButton.text")); // NOI18N
        alterarjButton.setFocusable(false);
        alterarjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        alterarjButton.setName("alterarjButton"); // NOI18N
        alterarjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(alterarjButton);

        excluirjButton.setBackground(resourceMap.getColor("excluirjButton.background")); // NOI18N
        excluirjButton.setIcon(resourceMap.getIcon("excluirjButton.icon")); // NOI18N
        excluirjButton.setText(resourceMap.getString("excluirjButton.text")); // NOI18N
        excluirjButton.setFocusable(false);
        excluirjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        excluirjButton.setName("excluirjButton"); // NOI18N
        excluirjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(excluirjButton);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        sairjButton.setBackground(resourceMap.getColor("sairjButton.background")); // NOI18N
        sairjButton.setIcon(resourceMap.getIcon("sairjButton.icon")); // NOI18N
        sairjButton.setText(resourceMap.getString("sairjButton.text")); // NOI18N
        sairjButton.setFocusable(false);
        sairjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        sairjButton.setName("sairjButton"); // NOI18N
        sairjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        sairjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(sairjButton);

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        bairrojTextField.setText(resourceMap.getString("bairrojTextField.text")); // NOI18N
        bairrojTextField.setName("bairrojTextField"); // NOI18N
        bairrojTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                bairrojTextFieldKeyReleased(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        paisjTextField1.setEditable(false);
        paisjTextField1.setText(resourceMap.getString("paisjTextField1.text")); // NOI18N
        paisjTextField1.setName("paisjTextField1"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        estadojComboBox.setName("estadojComboBox"); // NOI18N
        estadojComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                estadojComboBoxItemStateChanged(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        cidadejComboBox.setName("cidadejComboBox"); // NOI18N
        cidadejComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cidadejComboBoxActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        cidadejButton.setIcon(resourceMap.getIcon("cidadejButton.icon")); // NOI18N
        cidadejButton.setText(resourceMap.getString("cidadejButton.text")); // NOI18N
        cidadejButton.setContentAreaFilled(false);
        cidadejButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cidadejButton.setName("cidadejButton"); // NOI18N
        cidadejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cidadejButtonActionPerformed(evt);
            }
        });

        atualizaCidadejButton.setIcon(resourceMap.getIcon("atualizaCidadejButton.icon")); // NOI18N
        atualizaCidadejButton.setText(resourceMap.getString("atualizaCidadejButton.text")); // NOI18N
        atualizaCidadejButton.setContentAreaFilled(false);
        atualizaCidadejButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        atualizaCidadejButton.setName("atualizaCidadejButton"); // NOI18N
        atualizaCidadejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaCidadejButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(estadojComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bairrojTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(paisjTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(cidadejComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cidadejButton, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(atualizaCidadejButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(113, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cidadejButton)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addComponent(atualizaCidadejButton)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(estadojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cidadejComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bairrojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(paisjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setName("jPanel4"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        gradeBairrosjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#Id", "Bairro", "Cidade", "Estado", "Pais"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        gradeBairrosjTable.setName("gradeBairrosjTable"); // NOI18N
        gradeBairrosjTable.getTableHeader().setReorderingAllowed(false);
        gradeBairrosjTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                gradeBairrosjTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(gradeBairrosjTable);
        gradeBairrosjTable.getColumnModel().getColumn(0).setResizable(false);
        gradeBairrosjTable.getColumnModel().getColumn(0).setPreferredWidth(10);
        gradeBairrosjTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("gradeBairrosjTable.columnModel.title0")); // NOI18N
        gradeBairrosjTable.getColumnModel().getColumn(1).setResizable(false);
        gradeBairrosjTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("gradeBairrosjTable.columnModel.title1")); // NOI18N
        gradeBairrosjTable.getColumnModel().getColumn(2).setResizable(false);
        gradeBairrosjTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("gradeBairrosjTable.columnModel.title2")); // NOI18N
        gradeBairrosjTable.getColumnModel().getColumn(3).setResizable(false);
        gradeBairrosjTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("gradeBairrosjTable.columnModel.title3")); // NOI18N
        gradeBairrosjTable.getColumnModel().getColumn(4).setResizable(false);
        gradeBairrosjTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("gradeBairrosjTable.columnModel.title4")); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        ClassConecta conexao = new ClassConecta();

        ComboItem comboBairro;

        ComboItem comboEstado;

        public Integer codBairro;

//******************************************************************************************************
    public void getGradeBairros() throws ParseException{
       //Consultar Bairros
        Bairros oBairro = new Bairros();


        if ( bairrojTextField.getText().length() > 0 ){
            oBairro.setBairro(bairrojTextField.getText()+"%");
        }

        ResultSet resultSet = null;

        String[] colunasTabela = new String[]{"#Id", "Bairro", "Cidade", "Estado", "País"};
        DefaultTableModel modeloTabela = new DefaultTableModel(null, colunasTabela) {

            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        gradeBairrosjTable.setModel(modeloTabela);
        gradeBairrosjTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        gradeBairrosjTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        gradeBairrosjTable.getColumnModel().getColumn(4).setPreferredWidth(100);



try {


            resultSet = oBairro.getConsultar();

            if (resultSet.getRow() == 1){
             resultSet.first();
            }


        } catch (SQLException ex) {
            Logger.getLogger(Estados.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {

            while (resultSet.next()) {

                modeloTabela.addRow(new String[]{
                    Funcoes.getCodigoFormat(resultSet.getInt("COD_BAIRRO")),
                    resultSet.getString("BAIRRO"),
                    resultSet.getString("CIDADE") ,
                    resultSet.getString("ESTADO"),
                    resultSet.getString("PAIS"),
                });

            }

        } catch (SQLException ex) {
            Logger.getLogger(Estados.class.getName()).log(Level.SEVERE, null, ex);
        }

           }


    public void getFieldBairro(int codBairro) throws ParseException{

        Bairros oBairro = new Bairros();
        ResultSet rs = null;
        oBairro.setCod_Bairro(codBairro);

        try {
            rs = oBairro.getConsultar();
            rs.next();

            ComboItem oItem = new ComboItem(rs.getInt("COD_CIDADE"), rs.getString("CIDADE"));
            cidadejComboBox.setSelectedItem(oItem);

            //Pegar campos
            bairrojTextField.setText(rs.getString("BAIRRO"));
            cidadejComboBox.setSelectedItem(rs.getString("CIDADE"));

        } catch (SQLException ex) {
            Logger.getLogger(Estados.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:

        ClassConecta.conecta();

        salvarjButton.setEnabled(false);
        salvarMaisjButton.setEnabled(false);
        alterarjButton.setEnabled(false);
        excluirjButton.setEnabled(false);
        editarjButton.setEnabled(false);
        novojButton.setEnabled(true);

        bairrojTextField.grabFocus();

        atualizaCidadejButtonActionPerformed(null);
        buscarjButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowOpened

    private void sairjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairjButtonActionPerformed
        // TODO add your handling code here:
        if (!novojButton.isEnabled()){
                Object[] options = {"Sim", "Não"};
                int sd = JOptionPane.showOptionDialog(null, "Deseja realmente SAIR?", "Confirmação do Sistema!", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                if (sd != 0) {
                    return;
                }

                this.dispose();

            }else{

                this.dispose();

            }
    }//GEN-LAST:event_sairjButtonActionPerformed

    private void novojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButtonActionPerformed
        // TODO add your handling code here:
        novojButton.setEnabled(false);
        alterarjButton.setEnabled(false);
        editarjButton.setEnabled(false);
        excluirjButton.setEnabled(false);
        buscarjButton.setEnabled(true);
        salvarjButton.setEnabled(true);
        salvarMaisjButton.setEnabled(true);
        bairrojTextField.grabFocus();
        try {
            ClassConecta.con.setAutoCommit(false);
        } catch (SQLException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }
        bairrojTextField.setText("");
    }//GEN-LAST:event_novojButtonActionPerformed

    private void salvarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarjButtonActionPerformed
        // TODO add your handling code here:
            salvarjButton.setEnabled(false);
            salvarMaisjButton.setEnabled(false);
            alterarjButton.setEnabled(false);
            excluirjButton.setEnabled(false);
            novojButton.setEnabled(true);
            editarjButton.setEnabled(true);

            comboBairro = (ComboItem) cidadejComboBox.getSelectedItem();

            Bairros oBairro = new Bairros();

            if (bairrojTextField.getText().equals("")){

                JOptionPane.showMessageDialog(null, "O campo BAIRRO é obrigatório", "ATENÇÃO Mensagem do Sistema", JOptionPane.INFORMATION_MESSAGE);
                novojButtonActionPerformed(null);
            }else{

                oBairro.setBairro(bairrojTextField.getText().trim().toUpperCase());
                oBairro.setCod_Cidade(comboBairro.getId());
                oBairro.setCadastrar();
            }
        try {
            ClassConecta.con.commit();
            ClassConecta.con.setAutoCommit(true);
        } catch (SQLException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            getGradeBairros();
        } catch (ParseException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_salvarjButtonActionPerformed

    private void atualizaCidadejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizaCidadejButtonActionPerformed
        // TODO add your handling code here:
//        try {
//            Cidades oCidade = new Cidades();
//            ResultSet rs = null;
//
//            rs = oCidade.getConsultar();
//            cidadejComboBox.removeAllItems();
//            while (rs.next()) {
//                cidadejComboBox.addItem(new ComboItem(rs.getInt("COD_CIDADE"), rs.getString("CIDADE") +"  "+ rs.getString("SIGLA")));
//            }
//            cidadejComboBox.updateUI();
//        } catch (SQLException ex) {
//            Logger.getLogger(jFEstados.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_atualizaCidadejButtonActionPerformed

    private void cidadejComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cidadejComboBoxActionPerformed
        // TODO add your handling code here:
//        try {
//            Cidades oCidade = new Cidades();
//            ResultSet rs = null;
//
//            comboBairro = (ComboItem) cidadejComboBox.getSelectedItem();
//            oCidade.setCod_cidade(comboBairro.getId());
//            rs = oCidade.getConsultarCidade();
//
//            while (rs.next()){
//
//                paisjTextField1.setText(rs.getString("PAIS"));
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(jFCidades.class.getName()).log(Level.SEVERE, null, ex);
//        }

    }//GEN-LAST:event_cidadejComboBoxActionPerformed

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        try {
            // TODO add your handling code here:
            getGradeBairros();
        } catch (ParseException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Estados oEstado = new Estados();
            ResultSet rs = null;

            rs = oEstado.getConsultar();
            cidadejComboBox.removeAllItems();
            while (rs.next()) {
                estadojComboBox.addItem(new ComboItem(rs.getInt("COD_ESTADO"), rs.getString("ESTADO")));
            }
            estadojComboBox.updateUI();
        } catch (SQLException ex) {
            Logger.getLogger(jFEstados.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void editarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarjButtonActionPerformed
        // TODO add your handling code here:
            novojButton.setEnabled(false);
            salvarjButton.setEnabled(false);
            salvarMaisjButton.setEnabled(false);
            editarjButton.setEnabled(false);
            alterarjButton.setEnabled(true);
            excluirjButton.setEnabled(true);
        try {
            ClassConecta.con.setAutoCommit(false);
        } catch (SQLException ex) {
            Logger.getLogger(jFPessoas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_editarjButtonActionPerformed

    private void gradeBairrosjTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gradeBairrosjTableMouseClicked
        // TODO add your handling code here:
        editarjButton.setEnabled(true);
            codBairro = Integer.parseInt(gradeBairrosjTable.getValueAt(gradeBairrosjTable.getSelectedRow(), 0).toString());
            if (codBairro == 0) {
                return;
            }
        try {
            getFieldBairro(codBairro);
        } catch (ParseException ex) {
            Logger.getLogger(jFPaises.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_gradeBairrosjTableMouseClicked

    private void salvarMaisjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarMaisjButtonActionPerformed
        // TODO add your handling code here:
        salvarjButtonActionPerformed(null);
        novojButtonActionPerformed(null);
    }//GEN-LAST:event_salvarMaisjButtonActionPerformed

    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
        // TODO add your handling code here:
        alterarjButton.setEnabled(false);
        editarjButton.setEnabled(true);
        excluirjButton.setEnabled(false);
        novojButton.setEnabled(true);

        comboBairro = (ComboItem) cidadejComboBox.getSelectedItem();
        @SuppressWarnings("LocalVariableHidesMemberVariable")

        Bairros oBairro = new Bairros();
        oBairro.setCod_Bairro(codBairro);

        oBairro.setBairro(bairrojTextField.getText().trim().toUpperCase());
        oBairro.setCod_Cidade(comboBairro.getId());

        oBairro.setAlterar(conexao);
        try {
            ClassConecta.con.commit();
            ClassConecta.con.setAutoCommit(true);
        } catch (SQLException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            getGradeBairros();
        } catch (ParseException ex) {
            Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_alterarjButtonActionPerformed

    private void bairrojTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bairrojTextFieldKeyReleased
        // TODO add your handling code here:
        if (!alterarjButton.isEnabled()){
            try {
                Bairros oBairro = new Bairros();
                oBairro.setBairro(bairrojTextField.getText());
                ResultSet rs = null;
                rs = oBairro.getConsultar();
                getGradeBairros();
            } catch (ParseException ex) {
                Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(jFBairros.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (bairrojTextField.getText().equals("")){
            bairrojTextField.setText("");
            editarjButton.setEnabled(false);
        }
    }//GEN-LAST:event_bairrojTextFieldKeyReleased

    private void cidadejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cidadejButtonActionPerformed
        // TODO add your handling code here:
        jFCidades oFormCidade = new jFCidades();

        oFormCidade.setTitle("Cadastro de Cidades");
        oFormCidade.pack();
        oFormCidade.setLocationRelativeTo(null);
        oFormCidade.setModal(true);
        oFormCidade.setVisible(true);
        
    }//GEN-LAST:event_cidadejButtonActionPerformed

    private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
        // TODO add your handling code here:
        Object[] options = {"Sim", "Não"};
        int sd =
                JOptionPane.showOptionDialog(null, "Deseja realmente EXCLUIR esse registro?", "Confirmação do Sistema!", JOptionPane.DEFAULT_OPTION,
                JOptionPane.WARNING_MESSAGE, null, options, options[0]);

        if (sd != 0) {
            return;
        }
        alterarjButton.setEnabled(false);
        editarjButton.setEnabled(true);
        excluirjButton.setEnabled(false);
        novojButton.setEnabled(true);

        Bairros oBairro = new Bairros();
        oBairro.setCod_Bairro(codBairro);

        oBairro.setExcluir();
        try {
            ClassConecta.con.commit();
            ClassConecta.con.setAutoCommit(true);
        } catch (SQLException ex) {
            Logger.getLogger(jFPaises.class.getName()).log(Level.SEVERE, null, ex);
        }

            bairrojTextField.setText("");
        try {
            getGradeBairros();
        } catch (ParseException ex) {
            Logger.getLogger(jFPaises.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_excluirjButtonActionPerformed

    private void estadojComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_estadojComboBoxItemStateChanged
        // TODO add your handling code here:
        try {
            Cidades oCidade = new Cidades();
            ResultSet rs = null;

            comboEstado = (ComboItem) estadojComboBox.getSelectedItem();
            oCidade.setCod_estado(comboEstado.getId());
            rs = oCidade.getConsultarCidade();
            cidadejComboBox.removeAllItems();
            while (rs.next()) {
                cidadejComboBox.addItem(new ComboItem(rs.getInt("COD_CIDADE"), rs.getString("CIDADE") +"  "+ rs.getString("SIGLA")));
            }
            cidadejComboBox.updateUI();
        } catch (SQLException ex) {
            Logger.getLogger(jFEstados.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_estadojComboBoxItemStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jFBairros().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton atualizaCidadejButton;
    private javax.swing.JTextField bairrojTextField;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JButton cidadejButton;
    private javax.swing.JComboBox cidadejComboBox;
    private javax.swing.JButton editarjButton;
    private javax.swing.JComboBox estadojComboBox;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JTable gradeBairrosjTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton novojButton;
    private javax.swing.JTextField paisjTextField1;
    private javax.swing.JButton sairjButton;
    private javax.swing.JButton salvarMaisjButton;
    private javax.swing.JButton salvarjButton;
    // End of variables declaration//GEN-END:variables

}
